package cn.cttic.base.service.impl;

import cn.cttic.base.api.domain.BaseDevice;
import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.base.api.domain.bo.BaseFieldWorkProcessGroupBo;
import cn.cttic.base.api.domain.vo.BaseFieldWorkProcessGroupVo;
import cn.cttic.base.api.domain.BaseFieldWorkProcessGroup;
import cn.cttic.base.mapper.BaseFieldWorkProcessGroupMapper;
import cn.cttic.base.service.IBaseFieldWorkProcessGroupService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 外业规范Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
@RequiredArgsConstructor
@Service
public class BaseFieldWorkProcessGroupServiceImpl implements IBaseFieldWorkProcessGroupService {

    private final BaseFieldWorkProcessGroupMapper baseMapper;

    /**
     * 查询外业规范
     *
     * @param id 主键
     * @return 外业规范
     */
    @Override
    public BaseFieldWorkProcessGroupVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询外业规范列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 外业规范分页列表
     */
    @Override
    public TableDataInfo<BaseFieldWorkProcessGroupVo> queryPageList(BaseFieldWorkProcessGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseFieldWorkProcessGroup> lqw = buildQueryWrapper(bo);
        Page<BaseFieldWorkProcessGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的外业规范列表
     *
     * @param bo 查询条件
     * @return 外业规范列表
     */
    @Override
    public List<BaseFieldWorkProcessGroupVo> queryList(BaseFieldWorkProcessGroupBo bo) {
        LambdaQueryWrapper<BaseFieldWorkProcessGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseFieldWorkProcessGroup> buildQueryWrapper(BaseFieldWorkProcessGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseFieldWorkProcessGroup> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BaseFieldWorkProcessGroup::getCreateTime);
        lqw.like(StringUtils.isNotBlank(bo.getGroupName()), BaseFieldWorkProcessGroup::getGroupName, bo.getGroupName());
        lqw.eq(StringUtils.isNotBlank(bo.getGroupCode()), BaseFieldWorkProcessGroup::getGroupCode, bo.getGroupCode());
        lqw.eq(StringUtils.isNotBlank(bo.getGroupDescription()), BaseFieldWorkProcessGroup::getGroupDescription, bo.getGroupDescription());
        return lqw;
    }

    /**
     * 新增外业规范
     *
     * @param bo 外业规范
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseFieldWorkProcessGroupBo bo) {
        BaseFieldWorkProcessGroup add = MapstructUtils.convert(bo, BaseFieldWorkProcessGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改外业规范
     *
     * @param bo 外业规范
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseFieldWorkProcessGroupBo bo) {
        BaseFieldWorkProcessGroup update = MapstructUtils.convert(bo, BaseFieldWorkProcessGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseFieldWorkProcessGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除外业规范信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
